import SubListFactory from './SubListFactory';

export interface ILiList{
    anthologyTitle: string
    storeId: number
}

/**
 * deleteOwn: 删除自己的方法
 * id: 唯一标识符
 * isSelected: 是否被点击
 * isModificationMenuOpen: 是否显示修改菜单
 * onlyDisplayOwn: 将指定元素的isSelected变为true,其他元素的则为false
 * onlyDisplayModificationMenuOwn: isModificationMenuOpen,其他元素的则为false
 * noDisplay: 将所有的isModificationMenuOpen置为false
 * */
export interface IListFactoryElement extends ILiList {
    id: number
    isSelected: boolean
    storeId:number
    isModificationMenuOpen: boolean
    deleteOwn: () => void | undefined
    onlyDisplayOwn: () => void | undefined
    onlyDisplayModificationMenuOwn: () => void | undefined,
    subMenuList:SubListFactory
}

export default class ListFactory {
    private _list:IListFactoryElement[] = [];
    private id:number = 0;

    constructor() {

    }

    get list(): IListFactoryElement[] {
        return this._list;
    }

    /**
     * 初始化数组元素
     * */
    private initIListFactoryElement(value:ILiList):IListFactoryElement{
        const id = this.id++;
        return {
            anthologyTitle : value.anthologyTitle,
            id : id,
            storeId: value.storeId,
            isSelected : false,
            deleteOwn : this.deleteOwn(this._list, id),
            isModificationMenuOpen : false,
            onlyDisplayOwn : this.forEachIsSelected(id),
            onlyDisplayModificationMenuOwn : this.forEachIsModificationMenuOpen(id),
            subMenuList: new SubListFactory({anthologyTitle:value.anthologyTitle})
        }
    }

    /**
     * @param id
     * */
    private forEachIsSelected(id:number){
        return ()=>{
            this._list.forEach((val)=>{
                val.isSelected = val.id === id;
            })
        }
    }

    /**
     * @param id
     * */
    private forEachIsModificationMenuOpen(id:number){
        return ()=>{
            this._list.forEach((val)=>{
                val.isModificationMenuOpen = val.id === id;
            })
        }
    }

    /**
     * 找出被选中的元素的索引
     * */
    findSelectedIndex():number{
        for(let i = 0, length = this._list.length;i<length;i++){
            if(this._list[i].isSelected){
                return i;
            }
        }
        return -1;
    }

    private deleteOwn(list:IListFactoryElement[],id:number){
        return function () {
            list.forEach((val,index)=>{
                if(val.id === id){
                    list.splice(index,1);
                }
            })
        }
    }

    /**
     * 清理列表
     * */
    clearList(){
        this._list = [];
    }

    /**
     * 重新设置list
     * @param data{ILiList[]}
     * */
    againSetList(data:ILiList[]):IListFactoryElement[]{
        this.clearList();
        const LENGTH = data.length;
        for(let i=0;i<LENGTH;i++){
            this.push(this.initIListFactoryElement(data[i]));
        }
        return this._list;
    }

    noDisplayModificationMenu(){
        this._list.forEach((val)=>{
            val.isModificationMenuOpen = false;
        })
    }
    /**
     * @param value 添加的元素
     * */
    unshift(value:ILiList):IListFactoryElement[]{
        this._list.unshift(this.initIListFactoryElement(value));
        return this._list;
    }

    /**
     * 去掉指定的一个元素
     * @param index 要去掉元素的索引
     * */
    splice(index:number){
        this._list.splice(index,1);
        return this._list;
    }

    push(value:ILiList):IListFactoryElement[]{
        this._list.push(this.initIListFactoryElement(value));
        return this._list;
    }


}
